class CulturalHeritage::CulturalHeritage < ActiveRecord::Base
  default_scope where("cultural_heritage_cultural_heritages.deleted = 0")
#  default_scope where("deleted = 0")
  has_many :list_parts, :class_name => 'CulturalHeritage::Part',  :foreign_key => 'cultural_heritage'
  has_many :list_cultural_heritages, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'foundInId'
  has_many :list_avatars, :class_name => 'CulturalHeritage::CulturalHeritageAvatar',  :foreign_key => 'cultural_heritage'
  has_many :list_researches, :class_name => 'CulturalHeritage::CulturalHeritageFile',  :foreign_key => 'cultural_heritage'
  has_many :list_videos, :class_name => 'CulturalHeritage::CulturalHeritageYoutube',  :foreign_key => 'cultural_heritage'
  has_many :list_models, :class_name => 'CulturalHeritage::Model3D',  :foreign_key => 'cultural_heritage'
  has_many :list_researchers, :class_name => 'CulturalHeritage::Researcher',  :foreign_key => 'cultural_heritage'
  belongs_to :obj_collection, :class_name => 'CulturalHeritage::Collection',  :foreign_key => 'collection'
  belongs_to :obj_state, :class_name => 'CulturalHeritage::State',  :foreign_key => 'state'
  belongs_to :obj_group, :class_name => 'CulturalHeritage::Group',  :foreign_key => 'group'
  belongs_to :obj_subGroup, :class_name => 'CulturalHeritage::SubGroup',  :foreign_key => 'subGroup'
  belongs_to :obj_tipology, :class_name => 'CulturalHeritage::Tipology',  :foreign_key => 'tipology'
  #Revisar el caso de que sea mueble y esté en un inmueble o en una institucion cultural, implica polimorfismo
  #belongs_to :obj_located_at_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'cultural_heritage'
  belongs_to :obj_period, :class_name => 'CulturalHeritage::Period',  :foreign_key => 'period'
  belongs_to :obj_culture, :class_name => 'CulturalHeritage::Culture',  :foreign_key => 'culture'
  belongs_to :obj_style, :class_name => 'CulturalHeritage::Style',  :foreign_key => 'style'
  belongs_to :obj_owner, :class_name => 'Security::Person',  :foreign_key => 'owner'
  belongs_to :obj_author, :class_name => 'Security::Person',  :foreign_key => 'author'
  belongs_to :obj_conservational_state, :class_name => 'CulturalHeritage::ConservationalState',  :foreign_key => 'conservationalState'
  belongs_to :obj_integrity_state, :class_name => 'CulturalHeritage::IntegrityState',  :foreign_key => 'integrityState'
  belongs_to :obj_shape, :class_name => 'CulturalHeritage::Shape',  :foreign_key => 'shape'
  belongs_to :obj_technique, :class_name => 'CulturalHeritage::Technique',  :foreign_key => 'technique'
  belongs_to :obj_topic, :class_name => 'CulturalHeritage::Topic',  :foreign_key => 'topic'
  belongs_to :obj_typeCulturalInstitution, :class_name => 'CulturalHeritage::TypeCulturalInstitution',  :foreign_key => 'typeCulturalInstitution'
  belongs_to :obj_culturalInstitution, :class_name => 'CulturalHeritage::CulturalInstitution',  :foreign_key => 'culturalInstitution'
  belongs_to :obj_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'culturalInstitution'
  belongs_to :obj_found_in_cultural_heritage, :class_name => 'CulturalHeritage::CulturalHeritage',  :foreign_key => 'foundInId'
# Inicio de Validaciones
  validates :name, :presence => {:message => "El nombre es requerido"}
  validates :nationalRegistrationCode, :presence => {:message => "El Codigo de Registro Nacional es requerido"}  
  validates :subGroup, :presence => {:message => "El Sub Grupo es requerido"}
  validates :tipology, :presence => {:message => "La tipologia es requerida"}
  validates :typeCulturalInstitution, :presence => {:message => "El tipo de institucion cultural es requerido"}
  validates :culturalInstitution, :presence => {:message => "La institucion cultural es requerida"}
  validates :period, :presence => {:message => "El periodo es requeridp"}
  validates :culture, :presence => {:message => "La Cultura es requerida"}
  #por el pre-registro
#  validates :culture, :presence => {:message => "El Periodo es requerido"}
  validates :nationalRegistrationCode,:uniqueness => true
# Fin de Validaciones
  has_attached_file :avatar  
  acts_as_gmappable :lat => 'latitude', :lng =>'longitude',:process_geocoding => :geocode?
 

  def geocode?
    !(address.blank? || (!latitude.blank? && !longitude.blank?))
  end

  def gmaps4rails_address
  #describe how to retrieve the address from your model, if you use directly a db column, you can dry your code, see wiki
    "#{self.address}"
  end

  def gmaps4rails_sidebar
    "<span class='foo'>#{name}</span>" #put whatever you want here
  end

  def str_state
    if state.blank?
      return ""
    else
      obj_state = CulturalHeritage::State.find(state)
      return obj_state.name
    end
  end

  def str_group
    if group.blank?
      return ""
    else
      obj_group = CulturalHeritage::Group.find(group)
      return obj_group.name
    end
  end

  def str_subGroup
    if subGroup.blank?
      return ""
    else
      obj_subGroup = CulturalHeritage::SubGroup.find(subGroup)
      return obj_subGroup.name
    end
  end

  def str_tipology
    if tipology.blank?
      return ""
    else
      obj_tipology = CulturalHeritage::Tipology.find(tipology)
      return obj_tipology.name
    end
  end

  def str_typeCulturalInstitution
    if typeCulturalInstitution.blank?
      return ""
    else
      obj_typeCulturalInstitution = CulturalHeritage::TypeCulturalInstitution.find(typeCulturalInstitution)
      return obj_typeCulturalInstitution.name
    end
  end

  def str_culturalInstitution
    if culturalInstitution.blank?
      return ""
    else
      obj_culturalInstitution = CulturalHeritage::CulturalInstitution.find(culturalInstitution)
      return obj_culturalInstitution.name
    end
  end

  def str_period
    if period.blank?
      return ""
    else
      obj_period = CulturalHeritage::Period.find(period)
      return obj_period.name
    end
  end

  def str_culture
    if culture.blank?
      return ""
    else
      obj_culture = CulturalHeritage::Culture.find(culture)
      return obj_culture.name
    end
  end

  def str_style
    if style.blank?
      return ""
    else
      obj_style = CulturalHeritage::Style.find(style)
      return obj_style.name
    end
  end

  def str_owner
    if owner.blank?
      return ""
    else
      obj_person = Security::Person.find(owner)
      return obj_person.name
    end
  end

  def str_author
    if author.blank?
      return ""
    else
      obj_author = Security::Person.find(author)
      return obj_author.name
    end
  end

  def str_conservationalState
    if conservationalState.blank?
      return ""
    else
      obj_conservationalState = CulturalHeritage::ConservationalState.find(conservationalState)
      return obj_conservationalState.name
    end
  end

  def str_integrityState
    if integrityState.blank?
      return ""
    else
      obj_integrityState = CulturalHeritage::IntegrityState.find(integrityState)
      return obj_integrityState.name
    end
  end

  def str_shape
    if shape.blank?
      return ""
    else
      obj_shape = CulturalHeritage::Shape.find(shape)
      return obj_shape.name
    end
  end

  def str_technique
    if technique.blank?
      return ""
    else
      obj_technique = CulturalHeritage::Technique.find(technique)
      return obj_technique.name
    end
  end

  def str_topic
    if topic.blank?
      return ""
    else
      obj_topic = CulturalHeritage::Topic.find(topic)
      return obj_topic.name
    end
    
  end

  def str_foundInId
    if foundInId.blank?
      return ""
    else
      obj_mueble = CulturalHeritage::CulturalHeritage.find(foundInId)
      return obj_mueble.name
    end
  end

  def father_maximum_id
    if foundInId.blank?
      return -1
    else
      obj_cultural = CulturalHeritage::CulturalHeritage.find(foundInId)
      while ! obj_cultural.foundInId.blank?
        obj_cultural = CulturalHeritage::CulturalHeritage.find(obj_cultural.foundInId)
      end
      return obj_cultural.id
    end
  end

  def name_code
    return name+' ('+nationalRegistrationCode+')'
  end

  def title_name_code
    if group ==1
      return "[Pukara] Patrimonio Material Mueble " + name+' ('+nationalRegistrationCode+')'
    else
      return "[Pukara] Patrimonio Material Inmueble " + name+' ('+nationalRegistrationCode+')'
    end    
  end

  def list_nodes
    list = Array.new
    list.push(id)
    if ! foundInId.blank?          
      obj_cultural = CulturalHeritage::CulturalHeritage.find(foundInId)
      list.push(obj_cultural.id)
      while ! obj_cultural.foundInId.blank?
        list.push(obj_cultural.id)
        obj_cultural = CulturalHeritage::CulturalHeritage.find(obj_cultural.foundInId)
      end      
    end
    return list
  end

  def define_info_window_for_marker_for_maps(root_url)
    if (list_avatars.length == 0) or (avatar_file_name.blank?)
        image = root_url << "images/pukara_logo.png"
      else
        if ! avatar_file_name.blank?
          image = avatar.url
        else
          image = list_avatars.at(0).avatar.url
        end
      end
      if group == 1
        referencia = "<div><h2><a href = '/cultural_heritage/cultural_heritages/"
        if ! culturalInstitution.blank?
        institucion = "<h3><a href = '/cultural_heritage/cultural_institutions/"+
                    obj_culturalInstitution.id.to_s+"'>"+
                    obj_culturalInstitution.name.to_s+"</a></h3>"
        mensaje = "<p>Patrimonio ubicado en: <h3>" + institucion+ "</h3></p>"
        else
          mensaje =""
        end
      else
        referencia = "<div><h2><a href = '/cultural_heritage/cultural_heritage_properties/"
        mensaje = ""
      end
      return referencia+id.to_s+"'>"+name.to_s+"</a></h2><p>"+mensaje+"</p>"+
            "<p>"+description.to_s+"</p><img src="+image+
             "  style='thumb'"+"width='100' height='100'"+"/></div>"

   # "  style='thumb'"+"width='100' height='100'"+"/></div>"
#    referencia+id.to_s+"'>"+name.to_s+"</a></b>"+ "<br/>"+description.to_s+'<br/><br/> <img  src='+
#        image+'  width=\"100\" height=\"100\">'    
  end

  def show_url(root_url)
    pre_url = root_url+"cultural_heritage/"
    if group == 1
      pre_url = pre_url + "cultural_heritages/"
    else
      pre_url = pre_url + "cultural_heritage_properties/"
    end
    pre_url = pre_url + id.to_s
    return pre_url
  end
end
